package ink.xiaobaibai.controller;

import ink.xiaobaibai.annotation.OutsidePublicResources;
import ink.xiaobaibai.bo.DeviceMqttBo;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.serviceOxy.IMqttService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @description: 暴露出的MQTT指令接口
 * @author: 小白白
 * @create: 2021-08-20
 **/

@RestController
@RequestMapping("/mqtt")
@OutsidePublicResources
public class TestController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IMqttService mqttService;

    @GetMapping("/open")
    @ApiOperation("启动机器 || 定点,月费,购买一样的接口")
    public ResponseFormat openDevice(@RequestParam("deviceCode") String deviceCode,
                                     @RequestParam("fanTime") Integer fanTime,
                                     @RequestParam("startDelayedTime") Integer startDelayedTime,
                                     @RequestParam("valveTime") Integer valveTime,
                                     @RequestParam("workTime") Integer workTime) {
        if (this.stringRedisTemplate.hasKey("test_device_open")) {
            return ResponseFormat.fail("QPS:1");
        }
        this.mqttService.openDevice(deviceCode, new DeviceMqttBo(deviceCode, true, startDelayedTime, fanTime, valveTime, workTime));
        this.stringRedisTemplate.opsForValue().set("test_device_open", "1", 1, TimeUnit.SECONDS);
        return ResponseFormat.success("启动命令已发送");
    }

    @GetMapping("/close")
    @ApiOperation("关闭机器 || 定点,月费,购买")
    public ResponseFormat closeDevice(@RequestParam("deviceCode") String deviceCode) {
        if (this.stringRedisTemplate.hasKey("test_device_open")) {
            return ResponseFormat.fail("QPS:1");
        }
        this.mqttService.closeDevice(deviceCode);
        this.stringRedisTemplate.opsForValue().set("test_device_open", "1", 1, TimeUnit.SECONDS);
        return ResponseFormat.success("关闭命令已发送");
    }

    @GetMapping("/open-hotel")
    @ApiOperation("启动机器 || 酒店")
    public ResponseFormat openHotel(@RequestParam("deviceCode") String deviceCode,
                                    @RequestParam("fanTime") Integer fanTime,
                                    @RequestParam("startDelayedTime") Integer startDelayedTime,
                                    @RequestParam("valveTime") Integer valveTime,
                                    @RequestParam("workTime") Integer workTime,
                                    @RequestParam("roomNumber") String roomNumber) {
        if (this.stringRedisTemplate.hasKey("test_device_open")) {
            return ResponseFormat.fail("QPS:1");
        }
        this.mqttService.openDevice(deviceCode, new DeviceMqttBo(deviceCode, true, startDelayedTime, fanTime, valveTime, workTime, roomNumber));
        this.stringRedisTemplate.opsForValue().set("test_device_open", "1", 1, TimeUnit.SECONDS);
        return ResponseFormat.success("酒店机器启动命令已发送");
    }

    @GetMapping("/close-hotel")
    @ApiOperation("关闭机器 || 酒店")
    public ResponseFormat closeHotel(@RequestParam("deviceCode") String deviceCode,
                                     @RequestParam("roomNumber") String roomNumber) {
        if (this.stringRedisTemplate.hasKey("test_device_open")) {
            return ResponseFormat.fail("QPS:1");
        }
        this.mqttService.closeDevice(deviceCode, roomNumber);
        this.stringRedisTemplate.opsForValue().set("test_device_open", "1", 1, TimeUnit.SECONDS);
        return ResponseFormat.success("酒店机器关闭命令已发送");
    }

    @GetMapping("/check")
    @ApiOperation("检查接口")
    public ResponseFormat check(@RequestParam("deviceCode") String deviceCode) {
        if (this.stringRedisTemplate.hasKey("test_device_open")) {
            return ResponseFormat.fail("QPS:1");
        }
        this.mqttService.checkOnePayDevice(deviceCode);
        this.stringRedisTemplate.opsForValue().set("test_device_open", "1", 1, TimeUnit.SECONDS);
        return ResponseFormat.success("检查命令已发送");
    }

}
